import os
import pandas as pd
import gradio as gr
from openai import OpenAI
import requests

seg_api = "http://localhost:10086/segment"

def infer(query):
    segRequest = {
        "query": query,
    }

    try:
        # 发送 POST 请求到 FastAPI 服务
        response = requests.post(seg_api, json=segRequest)
        response.raise_for_status()  # 如果请求失败（4xx或5xx），抛出异常
        api_result = response.json()
        query_new = api_result['result']['query_new']
        # 将返回的JSON数据（列表字典）转换为Pandas DataFrame，Gradio可以优雅地显示
        # df_result = pd.DataFrame(api_result)
        for q in query_new.split('|'):
            print(q)

        return query_new
    except requests.exceptions.RequestException as e:
        # 处理请求错误
        return f"An error occurred: {e}"


def greet(query):
    return "res: " + infer(query)

demo = gr.Interface(
    fn=greet,
    inputs=[gr.Textbox(label="input", lines=3)],
    outputs=[gr.Textbox(label="result", lines=3)],
    title="Domain Classify Demo",
    description="输入句子，返回结果",
    examples=["打开车窗","播放音乐","导航回家","打开车窗打开前排空调温度调高点还是关了吧后排也要","明天南京有雨吗"]
)

demo.launch(server_name="0.0.0.0",server_port=10087)



# if __name__ == "__main__":
#     demo = gr.ChatInterface(infer).queue()
#     demo.launch(share=True, server_name="0.0.0.0")
